<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          default-expand-all
          :data="list"
          :props="{children:'children',label:'name'}"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit?'编辑部门':'新增部门'"
      :visible.sync="dialogVisible"
      @close="resetForm"
    >
      <!-- 解决不更新的问题  方法一v-if="      dialogVisible: false" 关闭和打开弹窗, 销毁和创建组件 -->
      <dept-dialog
        :id="curId"
        ref="addOrEditRef"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hSuccess"
        @close="dialogVisible=false"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranDataToTree } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      originList: [], // 保存所有部门列表的部分 {id,pid,name.code}
      isEdit: false, // false为新增 true 为编辑
      dialogVisible: false, //  弹框的显示与隐藏
      // 依赖一份树形数据
      list: [], // 保存部门列表,
      curId: ''
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 重置子组件表单
    resetForm() {
      // 通过在父组件中调用子组件中的重置方法resetForm实现重置表单
      this.$refs.addOrEditRef.resetForm()
    },
    hSuccess() {
      // 关闭弹窗
      this.dialogVisible = false
      // 重新渲染
      // this.loadDepartments()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('是否确定删除该部门?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartment(id)
        // 重新渲染
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hAdd(id) {
      this.dialogVisible = true
      this.curId = id
      this.isEdit = false
    },
    hEdit(id) {
      // this.$nextTick() 等待dom更新完成之后的回调函数,才会执行回调函数中的代码
      this.dialogVisible = true
      this.curId = id
      this.isEdit = true
      // dom 的更新是异步的
      this.$nextTick(() => {
        // 解决数据不更新的问题 方法二 this.$refs.addOrEditRef 可以获取子组件,可以直接操作子组件
        // console.log(this.$refs.addOrEditRef)
        this.$refs.addOrEditRef.loadDepartmentDetail()
      })
    },
    // 加载部门列表
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        // console.log(res.data.depts)
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
